package com.souku.finance.application.ui.more;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.souku.finance.application.R;
import com.souku.finance.application.base.BaseActivity;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ShareUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.Success;

import java.util.HashMap;

/**
 * Created by Android on 2016/12/22.
 */

public class FeedbackActivity extends BaseActivity implements View.OnClickListener {

    protected RelativeLayout topBack;
    protected TextView topTitle;
    protected EditText et;
    //    protected EditText etMobile;
//    protected LinearLayout llMobile;
    protected Button submit;
    protected TextView tvCount;
    private boolean isLogin;

    @Override
    public void internet() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.act_feedback);
        initView();
        topTitle.setText("反馈建议");
        isLogin = ShareUtil.getInstance().getString(Constant.Token) != null;
//        if (!isLogin) {
//            llMobile.setVisibility(View.VISIBLE);
//        }
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s != null) {
                    tvCount.setText(s.toString().length() + "");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.top_back) {
            finish();
        } else if (view.getId() == R.id.submit) {
//            String mobile = etMobile.getText().toString().trim();
//            if (!isLogin) {
//                if (MyTextUtil.isEmpty(mobile)) {
//                    ToastUtil.show("请输入手机号");
//                    return;
//                }
//                if (!MyTextUtil.isTelephone(mobile)) {
//                    ToastUtil.show("请输入正确的手机号");
//                    return;
//                }
//            }
            String content = et.getText().toString().trim();
            if (content.length() < 8) {
                ToastUtil.show("反馈内容不得少于8个字");
                return;
            }
            feedback(content);
        }
    }

    private void initView() {
        topBack = (RelativeLayout) findViewById(R.id.top_back);
        topBack.setOnClickListener(FeedbackActivity.this);
        topTitle = (TextView) findViewById(R.id.top_title);
        et = (EditText) findViewById(R.id.et);
//        etMobile = (EditText) findViewById(R.id.et_mobile);
//        llMobile = (LinearLayout) findViewById(R.id.ll_mobile);
        submit = (Button) findViewById(R.id.submit);
        submit.setOnClickListener(FeedbackActivity.this);
        tvCount = (TextView) findViewById(R.id.tv_count);
    }

    private void feedback(String content) {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("content", content);
//        if (!isLogin) {
//            map.put("mobile", mobile);
//        }
        RequestManager.getInstance().post(UrlConstact.FEEDBACK, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    ToastUtil.show("反馈成功");
                    finish();
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }
}
